package com.glq1218.service.impl;


import com.glq1218.domain.entity.User;
import com.glq1218.exception.CustomException;
import com.glq1218.security.CustomUserDetails;
import com.glq1218.service.CustomUserDetailsService;
import com.glq1218.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService, CustomUserDetailsService {
    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        // 查找用户
        User user = userService.getByUsername(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 查询对应的权限信息
//        List<String> perms = menuMapper.selectPermsByUserId(user.getId());
        return new CustomUserDetails(user);
    }

    public UserDetails loadUserByEmail(String email) {
        User user = userService.getByEmail(email);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("邮箱未注册");
        }
        return new CustomUserDetails(user);
    }

    @Override
    public UserDetails loadUserByPhone(String phone) {
        User user = userService.getByPhone(phone);
        if (Objects.isNull(user)) {
            throw new CustomException("手机号未注册");
        }
        return new CustomUserDetails(user);
    }
}
